﻿using System;
using System.Windows.Forms;
using System.Linq;
using StockLib;
using StockUI.Properties;

namespace StockUI
{
    public partial class DialogStockBlockList : DialogAbstract
    {
        public Stock Stock { get; set; }

        public DialogStockBlockList()
        {
            InitializeComponent();

            imageList.Images.Add(Resources.folder);
        }

        private void DialogStockBlockList_Load(object sender, System.EventArgs e)
        {
            if (Stock == null)
            {
                Stock = StockContainer.Instance.CurrentStock;
            }
            if (Stock == null)
            {

                throw new Exception("Stock对象为空");
            }

            this.Text += " - " + Stock.Name;

            BindDataSource();
        }

        private void BindDataSource()
        {
            listBlock.Items.Clear();
            foreach (StockBlock block in StockBlockGroupRegular.Instance.Values)
            {
                if (block.Values.Any(s => s.CodeKey == Stock.CodeKey))
                {
                    InsertListViewItem(block);
                }
            }
            foreach (StockBlock block in StockBlockGroupCustom.Instance.Values)
            {
                if (block.Values.Any(s => s.CodeKey == Stock.CodeKey))
                {
                    InsertListViewItem(block);
                }
            }
        }

        private void InsertListViewItem(StockBlock block)
        {
            ListViewItem item = new ListViewItem();
            item.IndentCount = 2;
            item.ImageIndex = 0;
            item.Text = string.Format("{0} ({1})", block.Name, block.StockNumber);
            item.Tag = block;
            listBlock.Items.Add(item);
        }

        private void listBlock_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBlock.SelectedItems.Count <= 0)
                return;

            if (!cbLink.Checked)
                return;

            FormAbstractParent parent = this.Owner as FormAbstractParent;
            StockBlock block = listBlock.SelectedItems[0].Tag as StockBlock;
            //SendMessage(parent.FindFormChild("Report"), new FormNotifyMessage("Report.Show", domain.Name, domain.CodeKey));
        }
    }
}
